package cn.iocoder.ny.module.mall.controller.admin.banner;


import cn.iocoder.ny.module.mall.domain.MallBanner;
import cn.iocoder.ny.module.mall.domain.MallBannerPageVo;
import cn.iocoder.ny.module.mall.domain.MallConfig;
import cn.iocoder.ny.module.mall.service.IMallBannerService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.Arrays;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;


/**
 * @author luchunhong
 * @date 2024/07/23/ 11:44
 * @description:首页Banner图
 */

@RestController
@RequestMapping("/banner")
public class MallBannerController{

    @Autowired
    private IMallBannerService iMallBannerService;



   /**
    * banner图列表 分页
    */

    @PostMapping("queryList")
    @PermitAll
    public CommonResult<PageResult<MallBanner>>  queryBannerList(@RequestBody MallBannerPageVo mallBanner){
        PageResult<MallBanner> mallBanners= iMallBannerService.getBannerPage(mallBanner);
        return success(mallBanners);
    }

    /**
     * 新增
     * @param
     * @return
     */
    @PostMapping("addBanner")
    @PermitAll
    public CommonResult<Boolean> addBanner(@RequestBody @Validated(MallBanner.Add.class) MallBanner mallBanner){
        LoginUser loginUser = getLoginUser();
        mallBanner.setTenantId(loginUser.getTenantId());
        // 检查是否只有一个字段有值
        String bannerUrl = mallBanner.getBannerUrl();
        Long productId = mallBanner.getProductId();
        if ((bannerUrl == null && productId == null) || (bannerUrl != null && productId != null)) {
            return CommonResult.error(500,"商品与公众号链接只允许存在一个");
        }
        iMallBannerService.save(mallBanner);
        return success(true);
    }

    /*
     根据ID查询
     */

    @GetMapping("queryById/{id}")
    @PermitAll
    public CommonResult queryById(@PathVariable("id") Long id){
        return success(iMallBannerService.queryById(id));
    }


    /*
    批量删除
     */

    @DeleteMapping("/{ids}")
    @PermitAll
    public CommonResult<Boolean> batchDelete(@PathVariable Long[] ids){
        iMallBannerService.removeByIds(Arrays.asList(ids));
        return success(true);
    }

    /*
    修改
     */
    @PostMapping("/update")
    @PermitAll
    public CommonResult<Boolean> updateBanner(@RequestBody @Validated(MallBanner.Edit.class) MallBanner mallBanner) {
        // 先获取 bannerUrl 和 productId 的值
        String router = mallBanner.getRouter();
        Long productId = mallBanner.getProductId();

        // 检查是否只有一个字段有值
        if ((router == null && productId == null) || (router != null && productId != null)) {
            return CommonResult.error(500, "商品与公众号链接只允许存在一个");
        }

        if (router != null) {
            mallBanner.setProductId(null);
        }
        if (productId != null) {
            mallBanner.setRouter(null);
        }
        iMallBannerService.updateById(mallBanner);
        return CommonResult.success(true);
    }

}
